package com.example.vietvoicecmdsimple;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.widget.Toast;

public class VietTTSEngine {
	public static final String BASE_API_PATH = "http://www.vnspeak.com/speakit.php?txt=";
	private static Context context;
	private static MediaPlayer mp;
	
	public static void Init(Context _context){
		context = _context;
	}
	
	public static void Speech(String text){
		String url;
		try {
			url = BASE_API_PATH + URLEncoder.encode(text, "UTF-8");
			mp = new MediaPlayer();
			mp.setAudioStreamType(AudioManager.STREAM_MUSIC);  
			mp.setDataSource(url);
			mp.prepare();
			mp.start();
			while(mp.isPlaying());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
		}
	}
}
